import { isNumber } from "../validator";

export function avg(...nums: number[]): number { return sum(...nums) / nums.length; }

export function sum(...nums: number[]): number { return nums.reduce((pre, cur) => pre + cur, 0); }

export function sqrSum(...nums: number[]): number { return nums.reduce((pre, cur) => pre + cur * cur, 0); }

export function randomRange(from: number, to: number): number {
    if (!isNumber(from) || !isNumber(to)) return NaN;
    return Math.min(from, to) + Math.random() * Math.abs(to - from);
}

export function randomInt(fromInt: number, toInt: number): number {
    if (!isNumber(fromInt) || !isNumber(toInt)) return NaN;
    let [floor, ceil] = [Math.ceil(fromInt), Math.floor(toInt)];
    if (floor > ceil) return NaN;
    else return Math.floor(Math.random() * (ceil - floor + 1)) + floor;
}

export function randomBool(percent: percent = 0.5): boolean { return !!(Math.random() < percent); }